package cn.servlet;

import cn.entity.User;
import cn.service.UserService;
import cn.service.impl.UserServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class RegisterServlet extends HttpServlet {
    private UserService userService = new UserServiceImpl();

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/html;charset=utf-8");
        User user = new User();
        user.setUsername(request.getParameter("name"));
        user.setPassword(request.getParameter("password"));
        user.setTelephone(request.getParameter("telephone"));
        Integer id = userService.register(user);
        if (0!=id){
            request.setAttribute("msg","注册成功");
            request.getRequestDispatcher("/login.jsp").forward(request,response);
        }else {
            response.sendRedirect("/register.jsp");
        }
    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf-8");
        doPost(request,response);
    }
}
